﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataAccess_Layer;
using Business_Layer;
using DTO;
using System.Collections.Generic;
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["NCC_UN"] != null)
        {
            this.Login1.Visible = false;
            this.LinkButtonLogOut.Text = "LogOut";
            this.LinkButtonName.Text = "Xin chào: " + Session["NCC_UN"].ToString();
            this.LinkButtonLogOut.Visible = true;
            this.LinkButtonName.Visible = true;
        }
        else
        {
            this.Login1.Visible = true;
            this.LinkButtonLogOut.Text = "";
            this.LinkButtonLogOut.Visible = false;
            this.LinkButtonName.Text = "";
            this.LinkButtonName.Visible = false;
        }
        

    }

    public string GetMD5Hash(string sPassword)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] hashedDataBytes = md5Hasher.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(sPassword));
        string sEncryptPass = Convert.ToBase64String(hashedDataBytes);
        return sEncryptPass;
    }
    NhaCungCapBUS nccBUS = new NhaCungCapBUS();
    NhaCungCap ncc = new NhaCungCap();
    protected void LoginButton_Click1(object sender, EventArgs e)
    {
        string mk = GetMD5Hash(Login1.Password);

        int kt1 = nccBUS.GetListAll(Login1.UserName, mk);
        if (kt1 < 1)
        {
            Response.Write("<script> alert('Tên đăng nhập không tồn tai hoặc Mật khẩu không đúng!!!!Vui lòng đăng ký lai.')</script>");
        }
        else
        {
            Session["NCC_UN"] = Login1.UserName;
            Response.Write("<script> alert('Chúc mừng bạn đã đăng nhập thành công!!!!')</script>");
          
            IList<NhaCungCapDTO> ds = nccBUS.GetList(Login1.UserName, mk);
            for (int i = 0; i < ds.Count; i++)
            {
                long id = ds[i].ID;
                Session["ID"] = id;
                //Session["NCC_UN"] = ds[i].Ten;
            }
            Response.Redirect("Default.aspx");
        }
    }
    protected void LinkButtonLogOut_Click(object sender, EventArgs e)
    {
        Session["NCC_UN"] = null;
        LinkButtonName.Text = LinkButtonLogOut.Text = "";
        Login1.Visible = true;
        LinkButtonName.Visible = false;
        Response.Redirect("Default.aspx");
    }
}
